package com.zhudeshuai.zhudemolist.animation;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.OvershootInterpolator;
import android.widget.Button;
import android.widget.ImageView;

import com.zhudeshuai.zhudemolist.R;

/**
 * ObjectAnimator学习
 *
 * @author zhudeshuai
 * @since 16/8/13
 */
public class ObjectAnimatorDemo extends Activity {

    private ImageView mIconIv;
    private Button mAlphaBt;

    private Button mSetBt;

    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_value_animator_demo);
        mIconIv = (ImageView) findViewById(R.id.iv_icon);
        mAlphaBt = (Button) findViewById(R.id.bt_alpha);
        mAlphaBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alpha();
            }
        });
        mSetBt = (Button) findViewById(R.id.bt_set);
        mSetBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setAnimation();
            }
        });

        mButton = (Button) findViewById(R.id.bt_translate);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                test();
            }
        });
    }

    private void test() {

        ObjectAnimator animatorX1 = ObjectAnimator.ofFloat(mIconIv,"translationX",100,200,50,150,80,100);
        animatorX1.setDuration(1000);
        animatorX1.setInterpolator(new OvershootInterpolator());
        animatorX1.start();
    }

    /**
     * 组合动画
     */
    private void setAnimation() {
        AnimatorSet animatorSet = new AnimatorSet();

        ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(mIconIv, "rotation", 0, 360);
        ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(mIconIv, "alpha", 1, 0);
        ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(mIconIv, "scaleX", 0, 1);
        ObjectAnimator objectAnimator4 = ObjectAnimator.ofFloat(mIconIv, "scaleY", 0, 1);

        ObjectAnimator objectAnimator5 = ObjectAnimator.ofFloat(mIconIv, "alpha", 0, 1);

        animatorSet.setDuration(3000);
        animatorSet.play(objectAnimator3).with(objectAnimator4);
        animatorSet.play(objectAnimator4).with(objectAnimator1);
        animatorSet.play(objectAnimator2).after(objectAnimator1);
        animatorSet.play(objectAnimator5).after(objectAnimator2);

//        animatorSet.playTogether(objectAnimator1, objectAnimator2, objectAnimator3, objectAnimator4);
        animatorSet.start();
    }

    private void alpha() {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mIconIv, "rotationX", 0, 180);
        objectAnimator.setDuration(1000);
        objectAnimator.start();
    }
}
